<template>
  <div class="login-container">
    <h2 class="login-title">{{ title }}</h2>

    <el-card class="login-card">
      <h2 class="account-login">账号登录</h2>
      <div class="count">
        <el-form
          label-position="top"
          :rules="rules"
          :model="form"
          ref="loginFormRef"
        >
          <el-form-item prop="username">
            <el-input
              v-model="form.username"
              placeholder="请输入用户名"
              :prefix-icon="User"
            ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input
              v-model="form.password"
              :show-password="true"
              placeholder="请输入密码"
              :prefix-icon="Lock"
            ></el-input>
          </el-form-item>

          <el-button :loading="loading" type="primary" @click="login"
            >登录</el-button
          >
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { User, Lock } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import useUserStore from '@/store/modules/user'
import { useRouter, useRoute } from 'vue-router'
import { ElNotification } from 'element-plus'
import { getTime } from '@/utils/time'

import type { loginForm } from '@/api/user/type'

const useStore = useUserStore()
const $router = useRouter()
const $route = useRoute()
const title = import.meta.env.VITE_APP_TITLE
const form: loginForm = reactive({
  username: 'admin',
  password: 'atguigu123'
})

const loginFormRef = ref()

const rules = {
  username: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 5, max: 10, message: '用户名长度5-10位', trigger: 'change' }
  ],
  password: [
    { required: true, message: '请输入密码', trigger: 'blur' },
    { min: 6, max: 20, message: '密码长度6-20位', trigger: 'change' }
  ]
}

const loading = ref(false)
const login = async () => {
  await loginFormRef.value.validate()

  loading.value = true
  try {
    await useStore.userLogin(form)
    const redirect = $route.query?.redirect
    $router.push({
      path: redirect || '/'
    })
    ElNotification({
      type: 'success',
      message: '欢迎回来',
      title: `HI,${getTime()}好`
    })
  } catch (error) {
    ElNotification({
      type: 'error',
      message: (error as Error).message
    })
  }
  loading.value = false
}
</script>

<style scoped lang="scss">
.login-container {
  width: 100%;
  height: 100vh;
  background: url('@/assets/images/bg/login.jpg') no-repeat;
  background-size: cover;
}
.login-title {
  padding-top: 15vh;
  font-size: 50px;
  color: #fff;
  font-weight: 700;
  text-align: center;
  letter-spacing: 10px;
}
.login-card {
  margin: 15vh auto 0;
  width: 400px;
  padding: 20px;
  color: #333;
  border-radius: 12px;

  button {
    padding: 10px 20px;
    height: auto;
    width: 100%;
    font-size: 22px;
    letter-spacing: 2px;
    background: linear-gradient(to right, #5793fc, #635aff);
    border: none;
  }

  .account-login {
    margin: 0px 0 30px;
    font-size: 32px;
    text-align: center;
  }
  :deep(.el-form-itemr) {
    margin-bottom: 36px;
  }
  :deep(.el-form-item__error) {
    font-size: 16px;
  }

  :deep(.el-input__wrapper) {
    box-shadow: none;
    border-radius: 0;
    border-bottom: 1px solid #ccc;
  }
  :deep(.el-form-item) {
    margin-bottom: 36px;
  }
  :deep(.el-input__inner) {
    padding-left: 40px;
    border-radius: 0;
    font-size: 20px;
    color: #333;
    background: none;
    border: none;
    // border-bottom: 1px solid #ccc;

    &:-webkit-autofill {
      background-color: #fff;
      background-image: none;
      box-shadow: 0 0 0px 1000px #fff inset !important;
      -webkit-text-fill-color: #333 !important;
    }

    /* placeholder */
    &::-webkit-input-placeholder {
      color: #fff;
    }

    &:-moz-placeholder {
      color: #fff;
    }

    &::-o-placeholder {
      color: #fff;
    }

    &:-ms-input-placeholder {
      color: #fff;
    }

    /* placeholder */
  }
  :deep(.el-input__icon) {
    font-size: 20px;
  }
}
</style>
